<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <style>
        * {
            list-style: none;
            margin: 0;
            padding: 0;
        }

        #shop-car li {
            border: 1px dotted red;
            margin: 20px 0;
            padding: 20px;
        }

        .container {
            width: 400px;
            padding: 20px;
        }
    </style>

    <div class="container">



        <ul id="shop-car">
            <!-- <li> -->
                <!-- <input type="checkbox" />
                <div>商品名称:</div>
                <img src="https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg" alt="">
                <div>商品价格:50</div>
                <div>购买数量:30</div>
                <div>商品规格: 黑</div>
                <div>是否选中：选中吧OR不选中</div> -->
            <!-- </li> -->
        </ul>
        <p id="totalnum">总数量：xxx</p>

    </div>
    <script src="./jquery.1.12.4.js"></script>
    <script>
        let products = [
            {
                goods_name: '小米10-1',
                goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
                goods_price: '500',
                goods_attr: '砂石黑4GB+64GB',
                goods_num: 10,
                goods_is_checked: true
            },
            {
                goods_name: '小米10-2',
                goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
                goods_price: '510',
                goods_attr: '砂石黑4GB+64GB',
                goods_num: 50,
                goods_is_checked: false
            }
        ];
        products.forEach(function (val, index) {
            $(`<li><input type="checkbox" />
                    <div>商品名称:${val.goods_name}</div>
                    <img src="${val.goods_img}"
                        alt="">
                    <div>商品价格:${val.goods_price}</div>
                    <div class = "goodsNum">购买数量:${val.goods_num}</div>
                    <div>商品规格:${val.goods_attr}</div>
                    <div class = "checked">是否选中:${val.goods_is_checked}</div></li>`).appendTo($("#shop-car"));
            $("input").eq(index).prop("checked", val.goods_is_checked);
        });
        //点击input改变是否选中框内的值
        $("input").on("click", function () {
            $(".checked").eq($(this).index("input")).text("是否选中:" + $(this).prop("checked"));
            fn();

        })
        //封装函数
        function fn() {
            //每次执行先清空num的值
            let num = 0;
            //所有选中的input的值相加
            $("input:checked").each(function (index, val) {
                num += parseInt($(val).siblings(".goodsNum").text().substr(5, 2));
            });
            //把相加得到的值赋值给P
            $("p").html("总数量：" + num);
        }
    </script>
    </script>
    </script>
</body>

</html>